// AWTTextComponent.h: interface for the AWTTextComponent class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_AWTTEXTCOMPONENT_H__632FD6DE_BB4C_4168_9D4A_1BEA68F358B1__INCLUDED_)
#define AFX_AWTTEXTCOMPONENT_H__632FD6DE_BB4C_4168_9D4A_1BEA68F358B1__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "AWTComponent.h"

class AFX_EXT_CLASS AWTTextComponent : public AWTComponent  
{
DECLARE_DYNAMIC(AWTTextComponent)
public:
	AWTTextComponent()
	{
		construct(CString());
	}
	AWTTextComponent(CString& text )
	{
		construct(text);
	}
	virtual ~AWTTextComponent()
	{
		if (m_textListener != NULL)
		{
			if (m_textListener->isMultiCaster())
			{
	#ifdef UNDER_CE
				AWTEventMulticaster* mc = (AWTEventMulticaster*)m_textListener;
	#else
				AWTEventMulticaster* mc = dynamic_cast<AWTEventMulticaster*>(m_textListener);
	#endif
				delete mc;
			}
		}
	}

  virtual void removeNotify();
	void setText(CString& t);
	CString& getText();

	CString& getSelectedText() 
	{
		m_string = getText().Mid(getSelectionStart(), getSelectionEnd() - getSelectionStart());
		return m_string;
  }

	BOOL isEditable() 
	{
		return m_editable;
	}

	void setEditable(BOOL b);

	void setBackground(AWTColor* c) 
	{
		backgroundSetByClientCode = TRUE;
    AWTComponent::setBackground(c);
  }

	int getSelectionStart();

	void setSelectionStart(int selectionStart) 
	{
		select(selectionStart, getSelectionEnd());
  }

	int getSelectionEnd();

	void setSelectionEnd(int selectionEnd) 
	{
		select(getSelectionStart(), selectionEnd);
  }

	void select(int selectionStart, int selectionEnd);
	
  void selectAll();

	void setCaretPosition(int position);

  int getCaretPosition();

	void addTextListener(AWTTextListener* l) 
	{
		if (l == NULL) 
		{
	    return;
		}
		m_textListener = AWTEventMulticaster::add(m_textListener, l);
//    newEventsOnly = true;
  }

	void removeTextListener(AWTTextListener* l) 
	{
		if (l == NULL) 
		{
			return;
		}
		m_textListener = AWTEventMulticaster::remove(m_textListener, l);
  }


	virtual BOOL isFocusTraversable()
		{return TRUE;}

	void setBorder(BOOL b)
		{m_border = b;}

	void processTextEvent(AWTTextEvent* e);

private:

	void construct(CString& text)
	{
		m_text = text;
		m_editable = TRUE;
		backgroundSetByClientCode = FALSE;
		m_textListener = NULL;
		m_selectionStart = 0;
		m_selectionEnd = 0;
		m_border = TRUE;
	}

protected:

	CString m_text;
	BOOL m_editable;
  int m_selectionStart;
  int m_selectionEnd;
  BOOL backgroundSetByClientCode;
  AWTTextListener* m_textListener;
	CString m_string;
	BOOL m_border;

};

#endif // !defined(AFX_AWTTEXTCOMPONENT_H__632FD6DE_BB4C_4168_9D4A_1BEA68F358B1__INCLUDED_)
